// !!common.checkAppResult?$("#header").hide():null; //此代码用来解决在部分APP下出现的双标题问题，如果需要解开本行注释即可
//吸顶菜单；
var $header=$('#header-title');
var $houseDescription=$('.house-description');
var houseDescriptionTop=$houseDescription.offset().top;
var $seeHouse=$('#seeHouse');
var $mapDetail=$('.map-detail');
var $takeLookAt=$('#takeLookAt');
common.goBackNext();
function _pressScroll(){
    $(window).on("scroll",function (e) {
        var opacityFilter=0;
        if($(this).scrollTop()<houseDescriptionTop){
            opacityFilter=($(this).scrollTop() / houseDescriptionTop).toFixed(3);
           opacityFilter=opacityFilter>0.9?0.9:opacityFilter;
            $header.css({opacity:opacityFilter});
            $header.find('h1').html('');
           $seeHouse.removeClass('active');
        }else if($(this).scrollTop()>$seeHouse.offset().top){
           $seeHouse.addClass('active');
           $header.find('h1').html($houseDescription.children('h3').html());
        }else{
            $header.find('h1').html($houseDescription.children('h3').html());
            $header.css({opacity:0.9});
        }
    });
}
//判断关注状态
var $likeAttention=$('.like-attention');
$likeAttention.off('touchend').on('touchend',function(e){
    var $this=$(this);
    var flag=$this.hasClass('attention');
    // console.log('关注')
    if(flag){
        $this.removeClass('attention');
        requestData({
            url:attentionUrl,
            sendData:{
                rewardId:rewardId,
                agentId:agentInfo.id,
                token:token,
                isAttention:0
            },
            callback:function(data){}
        });
    }else{
        $this.addClass('attention');
        requestData({
            url:attentionUrl,
            sendData:{
                rewardId:rewardId,
                agentId:agentInfo.id,
                token:token,
                isAttention:1
            },
            callback:function(data){}
        });
    }
    e.preventDefault();
    return false;
});
var $rewardsBtn=$('.rewards-btn');
var rewardId=common.getQueryString('id');
var token=common.getToken();
var agentInfo=common.getLocalData('H5-agentInfo');
var $sliderPanel=$(".slider-panel");
var $sliderImageList=$("#detail-images");
var globalHost=common.globalHost;
var imageHost=common.imageHost;
var getRewardDetail=common.getRewardDetail;
var url=globalHost+getRewardDetail;
var attentionUrl=globalHost+common.attention;//关注接口
var uncoverUrl=globalHost+common.uncover;//刮奖接口
var sendData={
    rewardId:rewardId,
    agentId:agentInfo.id,
    token:token
};
var agentPoints=0;
var isAuth=0;
var $viewTimes=$('#viewTimes');
var $attentionTimes=$('#attentionTimes');
var $lookedRecord=$('#lookedRecord');
requestData({
    url:url,
    sendData:sendData,
    callback:function(data){
        if(!!data && data['code']=="0"){
            data=data.res.data.record;
            latitude=data.latitude;
            longitude=data.longitude;
            var houseId=data['houseId'];
            _houseVisited({
                targetSelector:$takeLookAt,
                houseId:houseId,
                subVisitPoint:data['subVisitPoint'],
                subVisitCount:data['subVisitCount']
            })
            var salePhotoList=data['housePhotos'].split(",");
            common.buildNormalSwipe({
                sliderPanel:$sliderPanel,
                imgPath:imageHost,
                countingType:"number",
                data:salePhotoList,
                autoPlay:true
            });
            var rewardStatus=data['rewardStatus'];
            rewardStatus==1?$takeLookAt.show():"";
            var isAttention=data.isAttention;
            Number(isAttention)===1?$likeAttention.addClass('attention'):$likeAttention.removeClass('attention');
            var isView=data.isView;
            if(Number(isView)===1){
                $rewardsBtn.hide();
                $rewardsBtn.prev('.rewards-mask').hide();
                var tempDom='<p class="custome-price">房东底价&nbsp;<span class="min-total">'+data.basePrice+'万</span>赏金&nbsp;<span>'+data.rewardPrice+'元</span></p>'+
                        '<a id="dial" href="javascript:;" class="rewards-tel"><i></i>看房电话：<span>'+data.contactTel+'</span></a>';
                $cardDom.html(tempDom);
            }else{
                 //刮奖
                LuckyCard.case({
                    ratio: .3,
                    coverImg:'./img/out_mask_@3x.png'
                }, function() {
                    this.clearCover();
                    $(this.cover).remove();
                });
            }
            $.each(data,function (key,value) {
                $("#"+key).html(value.replace(common.regWarp,"<br/>"));
            });
            $lookedRecord.html('(已有'+data.viewTimes+'人刮开)');
            Number(data.viewTimes)>999?$viewTimes.html("999+"):'';
            Number(data.attentionTimes)>999?$attentionTimes.html("999+"):'';
            myMap.getPlace(longitude,latitude);
            _pressMap({
                targetSelector:$mapDetail,
                latitude:data.latitude,
                longitude:data.longitude,
                villageName:data.villageName,
            });
            agentPoints=Number(data.agentPoints);
            isAuth=Number(data.isAuth);
            _pressApplyHouse(rewardId,isAuth);
            _pressScroll();
        }
    }
});
/*房子被看跳转*/
function _houseVisited(options){
    var targetSelector=$(options.targetSelector);
    var houseId=options.houseId;
    var subVisitCount=options.subVisitCount;
    var subVisitPoint=options.subVisitPoint;
    targetSelector.off('tap').on('tap',function(e){
        window.location.href=encodeURI('./houseByVisited.html?houseId='+houseId+'&subVisitPoint='+subVisitPoint+'&subVisitCount='+subVisitCount);
    }) 
}
/*不同的状态不同的弹窗*/
var $maskControl=$('#mask');
var $alertControl=$('#alert');
var $alertSure=$('#alert-sure');//只有确定按钮的弹窗
var $alertContent=$alertControl.children('.alert-content');
var $cardDom=$('#card');//悬赏信息容器
$rewardsBtn.off('tap').on('tap',function(e){
    var $this=$(this);
    if(isAuth==1){
        $maskControl.removeClass('mask-hide').addClass("mask-show");
        $alertSure.removeClass('mask-hide').addClass("mask-show");
        $alertSure.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，请等待审核");
    }else if(isAuth==3){
        if(agentPoints<=0){
            $maskControl.removeClass('mask-hide').addClass("mask-show");
            $alertSure.removeClass('mask-hide').addClass("mask-show");
            $alertSure.children('.alert-content').html('每个经纪人每天可以刮开三次，明天再来看吧');
        }else{
            $this.prev().hide();
            $this.hide();
            requestData({
                url:uncoverUrl,
                sendData:{
                    rewardId:rewardId,
                    agentId:agentInfo.id,
                    token:token,
                },
                callback:function(data){
                    data=data.res.data.record;
                    // console.log(data);
                    var tempDom='<p class="custome-price">房东底价&nbsp;<span class="min-total">'+data.basePrice+'万</span>赏金&nbsp;<span>'+data.rewardPrice+'元</span></p>'+
                        '<a id="dial" href="javascript:;" class="rewards-tel"><i></i>看房电话：<span>'+data.contactTel+'</span></a>';
                    $cardDom.html(tempDom);
                }
            });
        }
    }else{
        $maskControl.removeClass('mask-hide').addClass("mask-show");
        $alertControl.removeClass('mask-hide').addClass("mask-show");
        $alertControl.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，快去认证吧！")
    }
    e.preventDefault();
    return false;
});
/*申请看房路由转发*/
function _pressApplyHouse(rewardId,isAuth){
    $seeHouse.off('touchend').on('touchend',function(e){
        var $this=$(this);
        if(isAuth==1){
            $maskControl.removeClass('mask-hide').addClass("mask-show");
            $alertSure.removeClass('mask-hide').addClass("mask-show");
            $alertSure.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，请等待审核");
        }else if(isAuth==3){
            window.location.href="askDoorPassStart.html?id="+rewardId;
        }else{
            $maskControl.removeClass('mask-hide').addClass("mask-show");
            $alertControl.removeClass('mask-hide').addClass("mask-show");
            $alertControl.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，快去认证吧！")
        }
        e.stopPropagation();
        e.preventDefault();
        return false;
    });
}
//点击取消按钮
 $alertControl.children('.alert-cancel').off('tap').on('tap',function(e){
    $maskControl.removeClass('mask-show').addClass("mask-hide");
    $alertControl.removeClass('mask-show').addClass("mask-hide");
    e.preventDefault();
    return false;
});
//点击确定按钮
$alertControl.children('.alert-sure').off('tap').on('tap',function(e){
    $maskControl.removeClass('mask-show').addClass("mask-hide");
    $alertControl.removeClass('mask-show').addClass("mask-hide");
    if(isAuth==2){
        window.location.href="registerFail.html";
    }else if(isAuth==0){
        window.location.href="registerInfoAgain.html";
    }else if(isAuth==1){
        window.location.href="registerAuth.html";
    }
    e.preventDefault();
    return false;
});
//只有确定按钮的不进行操作
$alertSure.children('.alert-sure').off('tap').on('tap',function(e){
    $maskControl.removeClass('mask-show').addClass("mask-hide");
    $alertSure.removeClass('mask-show').addClass("mask-hide");
    e.preventDefault();
    return false;
});
//调用拨号；
$cardDom.on('touchend','#dial',function(e){
    var $this=$(this);
    $this.attr('href','tel:'+$this.children('span').html());
})
function requestData(options) {
    var sendData=options.sendData;
    var url=options.url;
    var callback=options.callback;
    $.ajax({
        data:sendData,
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        dataType:"json",
        url:url,
        success:function(data){
            data=typeof data==="string"?data=JSON.parse(data):data;
            if(data.code==101 ||data.code==102){
                console.log("token为空或者token失效");
                window.location.href='login.html';
            };
            // console.log(data)
            callback(data);
        },
        timeout:10000,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
                common.errDialog("网络不畅，请稍后重试");
            
        }
    });
};
//点击调用地图；
function _pressMap(options){
    var $targetSelector=$(options.targetSelector);
    var longitude=options.longitude;
    var latitude=options.latitude;
    var villageName=options.villageName;
    $targetSelector.off('tap').on('tap',function(e){
        window.location.href="detail-map.html?longitude="+longitude+"&latitude="+latitude+"&villageName="+villageName;
        e.preventDefault();
        return false;
    });
}

